﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Habito.Helper;
using Newtonsoft.Json;
using Common;

namespace Habito.Admini
{
	public partial class Product : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(Ticket.UserName) || Session["username"] == null)
			{
				Response.Redirect("Login.aspx");
			}
			ViewState["classData"] = DataHelper.GetAllProductClass();
		}

		protected override void OnInit(EventArgs e)
		{
			if (Request.QueryString["Getall"] == "1")
			{
				String temp = DataHelper.GetAll();
				Response.Write(temp);
				Response.End();
			}

			if (Request.QueryString["GetProductsByClass"] == "1")
			{
				int id = int.Parse(Request.QueryString["id"]);
				Ajax.BLL.Products products = new Ajax.BLL.Products();
				Response.Write(JsonConvert.SerializeObject(products.GetModelList(" ClassID = " + id)));
				Response.End();
			}

			if (Request.QueryString["DeleteProduct"] == "1")
			{
				int id = int.Parse(Request.QueryString["id"].Replace("\'", ""));
				Ajax.BLL.Products c = new Ajax.BLL.Products();
				bool result = c.Delete(id);
				Response.Write(result);
				Response.End();
			}

			if (Request.QueryString["GetProductByID"] == "1")
			{
				int id = int.Parse(Request.QueryString["id"]);
				Ajax.BLL.Products product = new Ajax.BLL.Products();
				var pro = product.GetModel(id);
				Response.Write(JsonConvert.SerializeObject(pro));
				Response.End();
			}
			base.OnInit(e);
		}
	}
}